package com.training.java.core.homework08B;

import java.util.Date;
import java.util.List;

import junit.framework.TestCase;

import org.junit.Assert;
import org.junit.Test;

public class LaptopDAOJDBCImplTest {

	@Test
	public void testGetAllLaptops() throws Exception {
		
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:ORCL11B", "javatraining", "javatraining");
		
		List<Laptop> laptops = dao.getAllLaptops();
		
		Assert.assertNotNull(laptops);
		Assert.assertFalse(laptops.isEmpty());
		
		System.out.println("Printing Laptop ....");
		System.out.println("--------------------");
		for(Laptop laptop : laptops)
		{
			System.out.println("Model = " + laptop.getModel());
			System.out.println("Company = " + laptop.getCompany());
			System.out.println("Os = " + laptop.getOs());
			System.out.println("Processor = " + laptop.getProcessor());
			System.out.println("*************************");
		}
	}
	
	@Test
	public void testGetAllLaptopjoinowners() throws Exception {
		
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:ORCL11B", "javatraining", "javatraining");
		
		List<LaptopJoinOwner> laptopjoinowners = dao.getAllLaptopJoinOwners();
		
		Assert.assertNotNull(laptopjoinowners);
		Assert.assertFalse(laptopjoinowners.isEmpty());

		System.out.println("\n\n\n");
		System.out.println("Printing Laptop JOIN Laptop_owners ....");
		System.out.println("---------------------------------------");
		for(LaptopJoinOwner laptopjoinowner : laptopjoinowners)
		{
			System.out.println("Model = " + laptopjoinowner.getModel());
			System.out.println("Company = " + laptopjoinowner.getCompany());
			System.out.println("Os = " + laptopjoinowner.getOs());
			System.out.println("Processor = " + laptopjoinowner.getProcessor());
			System.out.println("Cellnumber = " + laptopjoinowner.getCellnumber());
			System.out.println("*************************");
		}
	}

	@Test
	public void getAllLaptopJoinOwnersPrep() throws Exception {
		
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:ORCL11B", "javatraining", "javatraining");
		
		List<LaptopJoinOwner> laptopjoinowners = dao.getAllLaptopJoinOwners();
		
		Assert.assertNotNull(laptopjoinowners);
		Assert.assertFalse(laptopjoinowners.isEmpty());

		System.out.println("\n\n\n");
		System.out.println("Printing Laptop JOIN Laptop_owners (Prepared)....");
		System.out.println("-------------------------------------------------");
		for(LaptopJoinOwner laptopjoinowner : laptopjoinowners)
		{
			System.out.println("Model = " + laptopjoinowner.getModel());
			System.out.println("Company = " + laptopjoinowner.getCompany());
			System.out.println("Os = " + laptopjoinowner.getOs());
			System.out.println("Processor = " + laptopjoinowner.getProcessor());
			System.out.println("Cellnumber = " + laptopjoinowner.getCellnumber());
			System.out.println("*************************");
		}
	}

	@Test
	public void testAddLaptop() throws DAOException  {
		
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:ORCL11B", "javatraining", "javatraining");
		
		Laptop laptop = new Laptop(1002003007, "PSI Data Systems", "Unix System 5", "Motorola 68030");
		
		dao.addLaptop(laptop);
		
		//verify that this book is added
		List<Laptop> laptops = dao.getAllLaptops();
		
		Assert.assertNotNull(laptops);
		Assert.assertFalse(laptops.isEmpty());

		System.out.println("\n\n\n");
		System.out.println("Printing Laptop after addition ....");
		System.out.println("-----------------------------------");
		for(Laptop laptop2 : laptops)
		{
			System.out.println("Model = " + laptop2.getModel());
			System.out.println("Company = " + laptop2.getCompany());
			System.out.println("Os = " + laptop2.getOs());
			System.out.println("Processor = " + laptop2.getProcessor());
			System.out.println("*************************");
		}
	}

	
	@Test
	public void testAddLaptopOwner() throws DAOException  {
		
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:ORCL11B", "javatraining", "javatraining");
		
		Laptopowner laptopowner = new Laptopowner(7185454324, 1002003004);
		
		dao.addLaptopOwner(laptopowner);
		
		//verify that this book is added
		List<Laptopowner> laptopowners = dao.getAllLaptopowners();
		
		Assert.assertNotNull(laptopowners);
		Assert.assertFalse(laptopowners.isEmpty());

		System.out.println("\n\n\n");
		System.out.println("Printing Laptop after addition ....");
		System.out.println("-----------------------------------");
		for(Laptopowner laptopowner2 : laptopowners)
		{
			System.out.println("Company = " + laptopowner2.getCellnumber());
			System.out.println("Model = " + laptopowner2.getModel());
			System.out.println("*************************");
		}
	}

	/**
	@Test
	public void testFindByPrimaryKey() throws DAOException
	{
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:XE", "bookstore", "password");
		
		Laptop book = dao.findByPrimaryKey(1);
		
		Assert.assertNotNull(book);
		Assert.assertEquals(1, book.getId());
		Assert.assertEquals("Dave Thomas", book.getAuthor());
	}
	
	@Test
	public void testUpdate() throws DAOException
	{
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:XE", "bookstore", "password");
		
		Laptop book = dao.findByPrimaryKey(2);
		
		book.setAuthor("Venkat");
		
		dao.updateLaptop(book);
		
		Laptop updatedBook = dao.findByPrimaryKey(2);
		
		Assert.assertNotNull(updatedBook);
		Assert.assertEquals("Venkat", updatedBook.getAuthor());
	}
	
	@Test
	public void testDelete()
	{
		LaptopDAO dao = new LaptopDAOJDBCImpl("jdbc:oracle:thin:@localhost:1521:XE", "bookstore", "password");
		
		try {
			dao.deleteLaptop(1);
		} catch (DAOException e) {
			TestCase.fail("Got DAOException while deleting");
		}
		
		try {
			dao.findByPrimaryKey(1);
			TestCase.fail("DAOException is expected here, but not thrown..So, delete must not be successful!!");
		} catch (DAOException e) {
			//expected
		}
	}
	**/


}
